package com.macro.mall.tiny.modules.bms.controller;

import com.macro.mall.tiny.common.api.CommonPage;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.modules.bms.dto.AdvertPositionQueryParam;
import com.macro.mall.tiny.modules.bms.model.AdvertPosition;
import com.macro.mall.tiny.modules.bms.service.AdvertPositionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Tag(name = "AdvertPositionController", description = "广告位管理")
@RestController
@RequestMapping("/advertPosition")
public class AdvertPositionController {

    @Autowired
    private AdvertPositionService advertPositionService;

    @Operation(summary = "根据ID获取广告位详情")
    @Parameters({
            @Parameter(name = "id", description = "广告位ID")
    })
    @GetMapping("/{id}")
    public CommonResult<AdvertPosition> getById(@PathVariable Long id) {
        AdvertPosition advertPosition = advertPositionService.getById(id);
        if (advertPosition == null || advertPosition.getDelFlag() == 1) {
            return CommonResult.failed("广告位不存在");
        }
        return CommonResult.success(advertPosition);
    }

    @Operation(summary = "分页获取广告位列表")
    @GetMapping("/list")
    public CommonResult<CommonPage<AdvertPosition>> getList(AdvertPositionQueryParam queryParam) {
        CommonPage<AdvertPosition> pageList = advertPositionService.getPageList(
                queryParam.getName(),
                queryParam.getStartTime(),
                queryParam.getEndTime(),
                queryParam.getPageNum(),
                queryParam.getPageSize());
        return CommonResult.success(pageList);
    }

    @Operation(summary = "新增广告位")
    @PostMapping("/create")
    public CommonResult<Object> create(@RequestBody AdvertPosition advertPosition) {
        boolean result = advertPositionService.create(advertPosition);
        if (result) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @Operation(summary = "更新广告位")
    @PutMapping("/update/{id}")
    public CommonResult<Object> update(@PathVariable Long id, @RequestBody AdvertPosition advertPosition) {
        boolean result = advertPositionService.update(id, advertPosition);
        if (result) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @Operation(summary = "删除广告位")
    @DeleteMapping("/delete/{id}")
    public CommonResult<Object> delete(@PathVariable Long id) {
        boolean result = advertPositionService.delete(id);
        if (result) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }
}